/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jumavidesktop.GUI.Mantenimientos.Usuarios.Vistas;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JTextField;
import jumavidesktop.Persistencia.entidades.Matricula;
import jumavidesktop.Persistencia.entidades.Usuario;

/**
 *
 * @author vimso
 */
public class PnelUsuario extends javax.swing.JPanel{
   
    public JTextField getJtApellidos() {
        return jtApellidos;
    }

    public void setJtApellidos(String jtApellidos) {
        this.jtApellidos.setText(jtApellidos);
    }

    public JTextField getJtDNI() {
        return jtDNI;
    }

    public void setJtDNI(String jtDNI) {
        this.jtDNI.setText(jtDNI);
    }

    public JTextField getJtMAtricula() {
        return jtMAtricula;
    }

    public void setJtMAtricula(String jtMAtricula) {
        this.jtMAtricula.setText(jtMAtricula);
    }

    public JTextField getJtMovil() {
        return jtMovil;
    }

    public void setJtMovil(String jtMovil) {
        this.jtMovil.setText(jtMovil);
    }

    public JTextField getJtNombre() {
        return jtNombre;
    }

    public void setJtNombre(String jtNombre) {
        this.jtNombre.setText(jtNombre);
    }

    public JTextField getJtTelefono() {
        return jtTelefono;
    }

    public void setJtTelefono(String jtTelefono) {
        this.jtTelefono.setText(jtTelefono);
    }

    public DefaultListModel getModelo() {
        return modelo;
    }
    
    public void setMatriculas(List<Matricula> mod){
       if (mod!=null) {
            for (Matricula lista : mod) {
                this.modelo.addElement(lista.getMatricula());
            }
        }
    }
    
    public List<Matricula> getMatriculas(){
        List<Matricula> lista = new ArrayList<Matricula>();
        int tam = modelo.getSize();
        while(tam>0){
            String valor = (String) modelo.getElementAt(tam-1);
            Matricula mat = new Matricula();
            System.out.println(valor);
            mat.setMatricula(valor);            
            lista.add(mat);
            tam--;
        }
        return lista;
    }
    
    public Usuario getUserPanel(){//devuelve un usuario con los datos de pantalla.
        Usuario useraux= new Usuario();
        useraux.setApellidos(jtApellidos.getText());
        useraux.setDni(jtDNI.getText());
        useraux.setMovil(jtMovil.getText());
        useraux.setNombre(jtNombre.getText());
        useraux.setTelefono(jtTelefono.getText());
        List<Matricula> matriculas= getMatriculas();
        
        for (Matricula matricula : matriculas) {
            matricula.setUsuario(useraux);
        }
        
        useraux.setMatriculaList(matriculas);
        useraux.setActivo(true);
        return useraux;
    }
   
    private DefaultListModel modelo;
    /**
     * Creates new form panelUsuario
     */
    public PnelUsuario() {
        initComponents();
        this.setVisible(true);
        modelo = new DefaultListModel();
        lsMatriculas.setModel(modelo);
    }
  
    public void borrarTodo(){
        modelo.removeAllElements();
        jtApellidos.setText("");
        jtDNI.setText("");
        jtMAtricula.setText("");
        jtMovil.setText("");
        jtNombre.setText("");
        jtTelefono.setText("");
        this.updateUI();
        this.repaint();
    }
    
    public void actualizarPanel(Usuario user){
        borrarTodo();
        setJtApellidos(user.getApellidos());
        setJtDNI(user.getDni());
        setJtMovil(user.getMovil());
        setMatriculas(user.getMatriculaList());
        setJtNombre(user.getNombre());
        setJtTelefono(user.getTelefono());
        this.updateUI();
        this.repaint();
    }


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        pnCajas = new javax.swing.JPanel();
        jlNombre = new javax.swing.JLabel();
        jtTelefono = new javax.swing.JTextField();
        jlApellidos = new javax.swing.JLabel();
        jlMovil = new javax.swing.JLabel();
        jtNombre = new javax.swing.JTextField();
        jlTelefono = new javax.swing.JLabel();
        jtApellidos = new javax.swing.JTextField();
        jtMovil = new javax.swing.JTextField();
        jlDNI = new javax.swing.JLabel();
        jtDNI = new javax.swing.JTextField();
        pnLista = new javax.swing.JPanel();
        pnSubLista = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lsMatriculas = new javax.swing.JList();
        jlNMatricula1 = new javax.swing.JLabel();
        btEliminar = new javax.swing.JButton();
        jtMAtricula = new javax.swing.JTextField();

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        setLayout(null);

        jlNombre.setText("Nombre:");

        jlApellidos.setText("Apellidos:");

        jlMovil.setText("Móvil:");

        jlTelefono.setText("Teléfono:");

        jlDNI.setText("D.N.I.:");

        javax.swing.GroupLayout pnCajasLayout = new javax.swing.GroupLayout(pnCajas);
        pnCajas.setLayout(pnCajasLayout);
        pnCajasLayout.setHorizontalGroup(
            pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnCajasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(pnCajasLayout.createSequentialGroup()
                            .addGroup(pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jtNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
                                .addComponent(jtApellidos))
                            .addContainerGap(20, Short.MAX_VALUE))
                        .addGroup(pnCajasLayout.createSequentialGroup()
                            .addGroup(pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jtDNI, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                .addComponent(jtMovil, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                .addComponent(jtTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 79, Short.MAX_VALUE)
                                .addGroup(pnCajasLayout.createSequentialGroup()
                                    .addGroup(pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jlMovil)
                                        .addComponent(jlDNI))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGap(111, 111, 111)))
                    .addComponent(jlNombre)
                    .addComponent(jlApellidos)
                    .addComponent(jlTelefono)))
        );
        pnCajasLayout.setVerticalGroup(
            pnCajasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnCajasLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlNombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlApellidos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jtApellidos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlTelefono)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlMovil)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtMovil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jlDNI)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtDNI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(46, 46, 46))
        );

        add(pnCajas);
        pnCajas.setBounds(0, 0, 200, 300);

        jScrollPane1.setViewportView(lsMatriculas);

        jlNMatricula1.setText("Matrículas:");

        btEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jumavidesktop/recursos/iconos/Eliminar.png"))); // NOI18N
        btEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEliminarActionPerformed(evt);
            }
        });

        jtMAtricula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jtMAtriculaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnSubListaLayout = new javax.swing.GroupLayout(pnSubLista);
        pnSubLista.setLayout(pnSubListaLayout);
        pnSubListaLayout.setHorizontalGroup(
            pnSubListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnSubListaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnSubListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnSubListaLayout.createSequentialGroup()
                        .addComponent(btEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnSubListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jtMAtricula, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)))
                    .addGroup(pnSubListaLayout.createSequentialGroup()
                        .addComponent(jlNMatricula1)
                        .addGap(0, 103, Short.MAX_VALUE)))
                .addContainerGap())
        );
        pnSubListaLayout.setVerticalGroup(
            pnSubListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnSubListaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jlNMatricula1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtMAtricula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnSubListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnSubListaLayout.createSequentialGroup()
                        .addComponent(btEliminar)
                        .addGap(202, 202, 202))
                    .addGroup(pnSubListaLayout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );

        javax.swing.GroupLayout pnListaLayout = new javax.swing.GroupLayout(pnLista);
        pnLista.setLayout(pnListaLayout);
        pnListaLayout.setHorizontalGroup(
            pnListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnListaLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(pnSubLista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(21, 21, 21))
        );
        pnListaLayout.setVerticalGroup(
            pnListaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnListaLayout.createSequentialGroup()
                .addComponent(pnSubLista, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        add(pnLista);
        pnLista.setBounds(200, 0, 200, 300);
    }// </editor-fold>//GEN-END:initComponents

private void jtMAtriculaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jtMAtriculaActionPerformed
    if(!jtMAtricula.getText().isEmpty()){
        modelo.addElement(jtMAtricula.getText());
        jtMAtricula.setText("");
        updateUI();
    }
}//GEN-LAST:event_jtMAtriculaActionPerformed

private void btEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEliminarActionPerformed
    int[] indexs = lsMatriculas.getSelectedIndices();
    int i=indexs.length;
    while(i>0){
        modelo.remove(indexs[i-1]);
        i--;
    }
    updateUI();
}//GEN-LAST:event_btEliminarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btEliminar;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel jlApellidos;
    private javax.swing.JLabel jlDNI;
    private javax.swing.JLabel jlMovil;
    private javax.swing.JLabel jlNMatricula1;
    private javax.swing.JLabel jlNombre;
    private javax.swing.JLabel jlTelefono;
    private javax.swing.JTextField jtApellidos;
    private javax.swing.JTextField jtDNI;
    private javax.swing.JTextField jtMAtricula;
    private javax.swing.JTextField jtMovil;
    private javax.swing.JTextField jtNombre;
    private javax.swing.JTextField jtTelefono;
    private javax.swing.JList lsMatriculas;
    private javax.swing.JPanel pnCajas;
    private javax.swing.JPanel pnLista;
    private javax.swing.JPanel pnSubLista;
    // End of variables declaration//GEN-END:variables
    }
